package com.central.apps.handler.MaterialInventory;

import cn.hutool.core.map.MapUtil;
import com.central.app.common.base.BaseHandler;
import com.central.app.common.constant.AppConstant;
import com.central.app.service.HandlerService;
import com.central.apps.dao.MaterialInventory.transferstoDao;
import com.central.apps.handler.base.LvBaseHandler;
import com.central.app.common.util.BLuntil;
import com.central.apps.service.IInventoryService;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import com.central.common.auth.details.LoginAppUser;
import com.central.common.exception.service.ServiceException;
import com.central.common.util.SysUserUtil;
import com.central.common.web.PageResult;
import com.central.common.web.Result;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.pdfbox.pdmodel.graphics.predictor.Up;
import org.elasticsearch.common.recycler.Recycler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerAdapter;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import static java.lang.Math.abs;
import static org.bouncycastle.asn1.x500.style.RFC4519Style.*;

/**
 * 库存调拨
 */
@Component
public class transferstoHandler extends BaseHandler {
    @Autowired
    private HandlerService handlerService;
    @Autowired
    private transferstoDao transferstoDao;


    @Override
    public void addDefault(String app, String object, Map<String, Object> defaultData) throws Exception {
        if(object.equals("gq_transferstoitem")){
            SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
            Date now =new Date();
            String transtime =sdf.format(now);
            defaultData.put("transtime",transtime);
        }
    }

    @Override
    public Object operate(String app, String type, Map<String, Object> params) throws Exception {
        int page = 1;
        int limit = 10;
        if (MapUtils.getInteger(params, AppConstant.BIZ_QUERY_PAGE) != null && MapUtils.getInteger(params, AppConstant.BIZ_QUERY_LIMIT) != null) {
            page = MapUtil.getInt(params, AppConstant.BIZ_QUERY_PAGE);
            limit = MapUtil.getInt(params, AppConstant.BIZ_QUERY_LIMIT);
        }
        if ("getInventory".equals(type)) {
            //选择库存余量大于0的物料
            String warehousenum = MapUtils.getString(params, "warehousenum");
           // String transferstonum = MapUtils.getString(params, "transferstonum");
            String transferstoid = MapUtils.getString(params, "transferstoid");
            String projectnum = MapUtils.getString(params, "projectnum");
            String targetdemandid= MapUtils.getString(params,"targetdemandid");
            String primarysubjectno = MapUtils.getString(params, "primarysubjectno");
            List<Map<String,Object>> list = new ArrayList<>();
            List<Map<String, Object>> inventoryList=null;
            List<Map<String, Object>> bySql = handlerService.findBySql("select subjectno from gq_transferstoitem where transferstoid='" + transferstoid + "'");
            if (CollectionUtils.isEmpty(bySql)){
                if (StringUtils.isNotEmpty(primarysubjectno)){
                    inventoryList = transferstoDao.getInventoryList(warehousenum, primarysubjectno,transferstoid,targetdemandid);//
                    if (CollectionUtils.isNotEmpty(inventoryList)){
                        for (int i=0;i<inventoryList.size();i++){
                            HashMap<String, Object> hashMap = new HashMap<>();
                            Map<String, Object> map = inventoryList.get(i);
                            String subjectno = MapUtils.getString(map, "subjectno");
                            Float avblbalance = MapUtils.getFloat(map, "avblbalance");
                            String unitcost = MapUtils.getString(map, "unitcost");
                            String proid = MapUtils.getString(map, "proid");
                            BigDecimal bigDecimal = new BigDecimal(unitcost);
                            BigDecimal bigDecimal1 = new BigDecimal(avblbalance);
                            BigDecimal outcost = new BigDecimal("0");
                            outcost=bigDecimal.multiply(bigDecimal1);
                            String itemnum = MapUtils.getString(map, "itemnum");
                            String itemdesc = MapUtils.getString(map, "itemdesc");
                            String specifications = MapUtils.getString(map, "specifications");
                            String model = MapUtils.getString(map, "model");
                            String binnum = MapUtils.getString(map, "binnum");
                            String batchnum = MapUtils.getString(map, "batchnum");
                            String id = MapUtils.getString(map, "id");
                            String orderunit = MapUtils.getString(map, "orderunit");
                            hashMap.put("avblbalance",avblbalance);
                            hashMap.put("subjectno",subjectno);
                            hashMap.put("unitcost",bigDecimal);
                            hashMap.put("outcost",outcost);
                            hashMap.put("proid",proid);
                            hashMap.put("itemnum",itemnum);
                            hashMap.put("itemdesc",itemdesc);
                            hashMap.put("specifications",specifications);
                            hashMap.put("model",model);
                            hashMap.put("orderunit",orderunit);
                            hashMap.put("binnum",binnum);
                            hashMap.put("batchnum",batchnum);
                            hashMap.put("id",id);
                            list.add(hashMap);
                        }
                    }
                    Long total = (long) inventoryList.size();
                }else {
                    inventoryList = transferstoDao.getInventoryList3(warehousenum,transferstoid,targetdemandid);//
                    if (CollectionUtils.isNotEmpty(inventoryList)){
                        for (int i=0;i<inventoryList.size();i++){
                            HashMap<String, Object> hashMap = new HashMap<>();
                            Map<String, Object> map = inventoryList.get(i);
                            String subjectno = MapUtils.getString(map, "subjectno");
                            String proid = MapUtils.getString(map, "proid");
                            Float avblbalance = MapUtils.getFloat(map, "avblbalance");
                            String unitcost = MapUtils.getString(map, "unitcost");
                            BigDecimal bigDecimal = new BigDecimal(unitcost);
                            BigDecimal bigDecimal1 = new BigDecimal(avblbalance);
                            BigDecimal outcost = new BigDecimal("0");
                            outcost=bigDecimal.multiply(bigDecimal1);
                            String itemnum = MapUtils.getString(map, "itemnum");
                            String itemdesc = MapUtils.getString(map, "itemdesc");
                            String specifications = MapUtils.getString(map, "specifications");
                            String model = MapUtils.getString(map, "model");
                            String binnum = MapUtils.getString(map, "binnum");
                            String batchnum = MapUtils.getString(map, "batchnum");
                            String id = MapUtils.getString(map, "id");
                            String orderunit = MapUtils.getString(map, "orderunit");
                            hashMap.put("avblbalance",avblbalance);
                            hashMap.put("subjectno",subjectno);
                            hashMap.put("proid",proid);
                            hashMap.put("unitcost",bigDecimal);
                            hashMap.put("outcost",outcost);
                            hashMap.put("itemnum",itemnum);
                            hashMap.put("itemdesc",itemdesc);
                            hashMap.put("specifications",specifications);
                            hashMap.put("model",model);
                            hashMap.put("orderunit",orderunit);
                            hashMap.put("binnum",binnum);
                            hashMap.put("batchnum",batchnum);
                            hashMap.put("id",id);
                            list.add(hashMap);
                        }
                    }
                    Long total = (long) inventoryList.size();
                }
            }else {
                Map<String, Object> map4 = bySql.get(0);
                String primarysubjectno1 = MapUtils.getString(map4, "subjectno");
                inventoryList = transferstoDao.getInventoryList(warehousenum, primarysubjectno1,transferstoid,targetdemandid);//
                if (CollectionUtils.isNotEmpty(inventoryList)){
                    for (int i=0;i<inventoryList.size();i++){
                        HashMap<String, Object> hashMap = new HashMap<>();
                        Map<String, Object> map = inventoryList.get(i);
                        String subjectno = MapUtils.getString(map, "subjectno");
                        Float avblbalance = MapUtils.getFloat(map, "avblbalance");
                        String unitcost = MapUtils.getString(map, "unitcost");
                        String proid = MapUtils.getString(map, "proid");
                        BigDecimal bigDecimal = new BigDecimal(unitcost);
                        BigDecimal bigDecimal1 = new BigDecimal(avblbalance);
                        BigDecimal outcost = new BigDecimal("0");
                        outcost=bigDecimal.multiply(bigDecimal1);
                        String itemnum = MapUtils.getString(map, "itemnum");
                        String itemdesc = MapUtils.getString(map, "itemdesc");
                        String specifications = MapUtils.getString(map, "specifications");
                        String model = MapUtils.getString(map, "model");
                        String binnum = MapUtils.getString(map, "binnum");
                        String batchnum = MapUtils.getString(map, "batchnum");
                        String id = MapUtils.getString(map, "id");
                        String orderunit = MapUtils.getString(map, "orderunit");
                        hashMap.put("avblbalance",avblbalance);
                        hashMap.put("subjectno",subjectno);
                        hashMap.put("unitcost",bigDecimal);
                        hashMap.put("outcost",outcost);
                        hashMap.put("proid",proid);
                        hashMap.put("itemnum",itemnum);
                        hashMap.put("itemdesc",itemdesc);
                        hashMap.put("specifications",specifications);
                        hashMap.put("model",model);
                        hashMap.put("orderunit",orderunit);
                        hashMap.put("binnum",binnum);
                        hashMap.put("batchnum",batchnum);
                        hashMap.put("id",id);
                        list.add(hashMap);
                    }
                }
                Long total = (long) inventoryList.size();
            }

            if (CollectionUtils.isNotEmpty(list)) {
                List<Map<String, Object>> collect = list.stream().skip((page - 1) * limit).limit(limit).collect(Collectors.toList());
                return PageResult.<Map<String, Object>>builder().data(collect).code(0).count((long) inventoryList.size()).build();
            }
        }else if ("getInventory1".equals(type)) {
            String warehousenum = MapUtils.getString(params, "warehousenum");
            // String transferstonum = MapUtils.getString(params, "transferstonum");
            String transferstoid = MapUtils.getString(params, "transferstoid");
            String projectnum = MapUtils.getString(params, "projectnum");
            String targetdemandid = MapUtils.getString(params, "targetdemandid");
            String primarysubjectno = MapUtils.getString(params, "primarysubjectno");
            String itemnumSearch = MapUtils.getString(params, "itemnum")==null?"":MapUtils.getString(params, "itemnum");
            String itemdescSearch = MapUtils.getString(params, "itemdesc")==null?"":MapUtils.getString(params, "itemdesc");
            String modelSearch = MapUtils.getString(params, "model")==null?"":MapUtils.getString(params, "model");
            List<Map<String, Object>> list = new ArrayList<>();
            //List<Map<String, Object>> bySql = handlerService.findBySql("select primarysubjectno from gq_transfersto where id='" + transferstoid + "'");
            List<Map<String, Object>> bySql = handlerService.findBySql("select subjectno from gq_transferstoitem where transferstoid='" + transferstoid + "'");
            if (CollectionUtils.isEmpty(bySql)){
                if (StringUtils.isNotEmpty(primarysubjectno)){
                    List<Map<String, Object>> inventoryList = transferstoDao.getInventoryList1(warehousenum, primarysubjectno, transferstoid,targetdemandid);//
                    if (CollectionUtils.isNotEmpty(inventoryList)) {
                        inventoryList = inventoryList.stream().filter(stringObjectMap ->
                                MapUtils.getString(stringObjectMap,"itemnum").contains(itemnumSearch) &&
                                        MapUtils.getString(stringObjectMap,"itemdesc").contains(itemdescSearch) &&
                                        MapUtils.getString(stringObjectMap,"model").contains(modelSearch)
                        ).collect(Collectors.toList());
                        for (int i = 0; i < inventoryList.size(); i++) {
                            HashMap<String, Object> hashMap = new HashMap<>();
                            Map<String, Object> map = inventoryList.get(i);
                            String subjectno = MapUtils.getString(map, "subjectno");
                            String proid = MapUtils.getString(map, "proid");
                            Float avblbalance = MapUtils.getFloat(map, "avblbalance");
                            String unitcost = MapUtils.getString(map, "unitcost");
                            BigDecimal bigDecimal = new BigDecimal(unitcost);
                            BigDecimal bigDecimal1 = new BigDecimal(avblbalance);
                            BigDecimal outcost = new BigDecimal("0");
                            outcost = bigDecimal.multiply(bigDecimal1);
                            String itemnum = MapUtils.getString(map, "itemnum");
                            String itemdesc = MapUtils.getString(map, "itemdesc");
                            String specifications = MapUtils.getString(map, "specifications");
                            String model = MapUtils.getString(map, "model");
                            String binnum = MapUtils.getString(map, "binnum");
                            String batchnum = MapUtils.getString(map, "batchnum");
                            String id = MapUtils.getString(map, "id");
                            String orderunit = MapUtils.getString(map, "orderunit");
                            hashMap.put("avblbalance", avblbalance);
                            hashMap.put("subjectno", subjectno);
                            hashMap.put("proid",proid);
                            hashMap.put("unitcost", bigDecimal);
                            hashMap.put("outcost", outcost);
                            hashMap.put("itemnum", itemnum);
                            hashMap.put("itemdesc", itemdesc);
                            hashMap.put("specifications", specifications);
                            hashMap.put("model", model);
                            hashMap.put("orderunit", orderunit);
                            hashMap.put("binnum", binnum);
                            hashMap.put("batchnum", batchnum);
                            hashMap.put("id", id);
                            list.add(hashMap);
                        }
                    }
                }else {
                    List<Map<String, Object>> inventoryList = transferstoDao.getInventoryList2(warehousenum, transferstoid,targetdemandid);//
                    if (CollectionUtils.isNotEmpty(inventoryList)) {
                        inventoryList = inventoryList.stream().filter(stringObjectMap ->
                                MapUtils.getString(stringObjectMap,"itemnum").contains(itemnumSearch) &&
                                        MapUtils.getString(stringObjectMap,"itemdesc").contains(itemdescSearch) &&
                                        MapUtils.getString(stringObjectMap,"model").contains(modelSearch)
                        ).collect(Collectors.toList());
                        for (int i = 0; i < inventoryList.size(); i++) {
                            HashMap<String, Object> hashMap = new HashMap<>();
                            Map<String, Object> map = inventoryList.get(i);
                            String subjectno = MapUtils.getString(map, "subjectno");
                            String proid = MapUtils.getString(map, "proid");
                            Float avblbalance = MapUtils.getFloat(map, "avblbalance");
                            String unitcost = MapUtils.getString(map, "unitcost");
                            BigDecimal bigDecimal = new BigDecimal(unitcost);
                            BigDecimal bigDecimal1 = new BigDecimal(avblbalance);
                            BigDecimal outcost = new BigDecimal("0");
                            outcost = bigDecimal.multiply(bigDecimal1);
                            String itemnum = MapUtils.getString(map, "itemnum");
                            String itemdesc = MapUtils.getString(map, "itemdesc");
                            String specifications = MapUtils.getString(map, "specifications");
                            String model = MapUtils.getString(map, "model");
                            String binnum = MapUtils.getString(map, "binnum");
                            String batchnum = MapUtils.getString(map, "batchnum");
                            String id = MapUtils.getString(map, "id");
                            String orderunit = MapUtils.getString(map, "orderunit");
                            hashMap.put("avblbalance", avblbalance);
                            hashMap.put("subjectno", subjectno);
                            hashMap.put("proid",proid);
                            hashMap.put("unitcost", bigDecimal);
                            hashMap.put("outcost", outcost);
                            hashMap.put("itemnum", itemnum);
                            hashMap.put("itemdesc", itemdesc);
                            hashMap.put("specifications", specifications);
                            hashMap.put("model", model);
                            hashMap.put("orderunit", orderunit);
                            hashMap.put("binnum", binnum);
                            hashMap.put("batchnum", batchnum);
                            hashMap.put("id", id);
                            list.add(hashMap);
                        }
                    }
                }
            }else {
                Map<String, Object> map4 = bySql.get(0);
                String primarysubjectno1 = MapUtils.getString(map4, "subjectno");
                List<Map<String, Object>> inventoryList = transferstoDao.getInventoryList(warehousenum, primarysubjectno1,transferstoid,targetdemandid);//
                if (CollectionUtils.isNotEmpty(inventoryList)){
                    inventoryList = inventoryList.stream().filter(stringObjectMap ->
                            MapUtils.getString(stringObjectMap,"itemnum").contains(itemnumSearch) &&
                                    MapUtils.getString(stringObjectMap,"itemdesc").contains(itemdescSearch) &&
                                    MapUtils.getString(stringObjectMap,"model").contains(modelSearch)
                    ).collect(Collectors.toList());
                    for (int i=0;i<inventoryList.size();i++){
                        HashMap<String, Object> hashMap = new HashMap<>();
                        Map<String, Object> map = inventoryList.get(i);
                        String subjectno = MapUtils.getString(map, "subjectno");
                        Float avblbalance = MapUtils.getFloat(map, "avblbalance");
                        String unitcost = MapUtils.getString(map, "unitcost");
                        String proid = MapUtils.getString(map, "proid");
                        BigDecimal bigDecimal = new BigDecimal(unitcost);
                        BigDecimal bigDecimal1 = new BigDecimal(avblbalance);
                        BigDecimal outcost = new BigDecimal("0");
                        outcost=bigDecimal.multiply(bigDecimal1);
                        String itemnum = MapUtils.getString(map, "itemnum");
                        String itemdesc = MapUtils.getString(map, "itemdesc");
                        String specifications = MapUtils.getString(map, "specifications");
                        String model = MapUtils.getString(map, "model");
                        String binnum = MapUtils.getString(map, "binnum");
                        String batchnum = MapUtils.getString(map, "batchnum");
                        String id = MapUtils.getString(map, "id");
                        String orderunit = MapUtils.getString(map, "orderunit");
                        hashMap.put("avblbalance",avblbalance);
                        hashMap.put("subjectno",subjectno);
                        hashMap.put("unitcost",bigDecimal);
                        hashMap.put("outcost",outcost);
                        hashMap.put("proid",proid);
                        hashMap.put("itemnum",itemnum);
                        hashMap.put("itemdesc",itemdesc);
                        hashMap.put("specifications",specifications);
                        hashMap.put("model",model);
                        hashMap.put("orderunit",orderunit);
                        hashMap.put("binnum",binnum);
                        hashMap.put("batchnum",batchnum);
                        hashMap.put("id",id);
                        list.add(hashMap);
                    }
                }
            }


            if (CollectionUtils.isNotEmpty(list)) {
                List<Map<String, Object>> collect = list.stream().skip((page - 1) * limit).limit(limit).collect(Collectors.toList());
                return PageResult.<Map<String, Object>>builder().data(collect).code(0).count((long) list.size()).build();
            }
        }
        return null;
    }

    @Override
    public String beforeExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op) throws Exception {
        String msg ="";
        if(flowNode.getId().equals("submit")){
            List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_transfersto where id ="+id);
            String transferstoid = MapUtils.getString(bySql.get(0), "id");
//            List<Map<String, Object>> bySql3 = handlerService.findBySql("SELECT * from biz_attribute where object_id=(SELECT id from biz_object where name ='gq_transferstoitem') and required !=''");
//            if (CollectionUtils.isNotEmpty(bySql3)){
//                for(int i=0;i<bySql3.size();i++){
//                    Map<String, Object> map = bySql3.get(i);
//                    String required = MapUtils.getString(map, "required");
//                    if (required!=null||required!=""){
//                        //log.info("required+++++++++++++++++++++++++++++++++++++" + required);
//                        String columnname = MapUtils.getString(bySql3.get(i), "columnname");
//                        // log.info("required+++++++++++++++++++++++++++++++++++++" + columnname);
//                        List<Map<String, Object>> bySql4 = handlerService.findBySql("select "+columnname+" from gq_transferstoitem where transferstoid='" +id+ "'");
//                        if (CollectionUtils.isNotEmpty(bySql4)){
//                            Map<String, Object> map1 = bySql4.get(0);
//                            String columnname1 = MapUtils.getString(map1, columnname);
//                            if (StringUtils.isEmpty(columnname1)){
//                                return "有必填信息未填，请继续填写!";
//                            }
//                        }
//                    }
//                }
//            }
            // String transferstonum =MapUtils.getString(bySql.get(0),"transferstonum");
            String outwarehousenum =MapUtils.getString(bySql.get(0),"outwarehousenum");//调出库房编码
            String primarysubjectno =MapUtils.getString(bySql.get(0),"primarysubjectno");//课题号
            String inwarehousenum = MapUtils.getString(bySql.get(0), "inwarehousenum");//调入库房
            String transferapplicationtype = MapUtils.getString(bySql.get(0), "transferapplicationtype");//类型
            List<Map<String, Object>> bySql5 = handlerService.findBySql("SELECT waregrade from gq_store where storenum='" + outwarehousenum + "'");
            String outwaregrade="";//调出库房等级
            if (CollectionUtils.isNotEmpty(bySql5)){
                outwaregrade = MapUtils.getString(bySql5.get(0), "waregrade");
            }
            List<Map<String, Object>> bySql6 = handlerService.findBySql("SELECT waregrade from gq_store where storenum='" + inwarehousenum + "'");
            String inwaregrade="";//调入库房等级
            if (CollectionUtils.isNotEmpty(bySql6)){
                inwaregrade = MapUtils.getString(bySql6.get(0), "waregrade");
            }
            if (transferapplicationtype.equals("调拨")){
                if (inwaregrade.equals("部门库房")&&outwaregrade.equals("部门库房")){

                }else {
                    return "当类型为调拨时，只能由部门库房向部门库房调拨";
                }
            }else if (transferapplicationtype.equals("返库")){
                if (inwaregrade.equals("中心库房")&&outwaregrade.equals("部门库房")){

                }else {
                    return "当类型为返库时，只能由部门库房向中心库房返库";
                }
            }
            String menutype = MapUtils.getString(bySql.get(0), "menutype");
            if (menutype.equals("1")) {
                List<Map<String, Object>> bySql1 = handlerService.findBySql("select * from gq_transferstoitem where transferstoid ='" + transferstoid + "'");
                if (CollectionUtils.isNotEmpty(bySql1)) {
                    for (int i = 0; i < bySql1.size(); i++) {
                        Map<String, Object> map = bySql1.get(i);
                        String itemnum = MapUtils.getString(map, "itemnum");
                        String fromlot = MapUtils.getString(map, "fromlot");//原批次
                        float thenum = MapUtils.getFloat(map, "thenum");//调拨数量
                        Float inventory = MapUtils.getFloat(map, "Inventory");//可用余量
                        float thenum2 = 0;
                        List<Map<String, Object>> bySql7 = handlerService.findBySql("select t.* from gq_transferstoitem t left join gq_transfersto a on t.transferstoid=a.id where a.outwarehousenum='"+outwarehousenum+"' and  t.itemnum ='" + itemnum + "'  and t.fromlot ='" + fromlot + "' and a.status!='新建' and a.status!='已作废' and a.status!='审批通过'");
                        if (CollectionUtils.isNotEmpty(bySql7)) {
                            for (int j = 0; j < bySql7.size(); j++) {
                                Map<String, Object> map1 = bySql7.get(j);
                                Float thenum1 = MapUtils.getFloat(map1, "thenum");
                                thenum2 = thenum1 + thenum2;
                            }
                            thenum=thenum+thenum2;
                        }
                            /*
                                加上主表的中心库房，课题号进行过滤库存余量。（不能只对批次+物料编码过滤）
                             */
                            List<Map<String, Object>> bySql3 = handlerService.findBySql("select * from gq_invbalances where itemnum='" + itemnum + "' and batchnum='" + fromlot + "' and subjectno = '" + primarysubjectno + "' and storenum ='" + outwarehousenum + "'");
                            Float avblbalance = (float) 0;
                            if (CollectionUtils.isNotEmpty(bySql3)) {
                                Map<String, Object> map1 = bySql3.get(0);
                                avblbalance = MapUtils.getFloat(map1, "avblbalance");
                            }
//                            List<Map<String, Object>> bySql2 = handlerService.findBySql("select t.* from gq_transferstoitem t left join gq_transfersto a on t.transferstoid=a.id where itemnum ='" + itemnum + "'  and fromlot ='" + fromlot + "' and a.status!='审批通过'");
//                            if (CollectionUtils.isNotEmpty(bySql2)){
//                                List<Map<String, Object>> bySql4 = handlerService.findBySql("select t.* from gq_transferstoitem t left join gq_transfersto a on t.transferstoid=a.id where itemnum ='" + itemnum + "'  and fromlot ='" + fromlot + "' and a.status!='审批通过' and a.status!='新建' and a.status!='待审核'");
//                                if (CollectionUtils.isNotEmpty(bySql4)){
//                                    float quantity3=0;
//                                    for (int z=0;z<bySql4.size();z++){
//                                        Map<String, Object> map2 = bySql4.get(z);
//                                        Float thenum1 = MapUtils.getFloat(map2, "thenum");
//                                        quantity3=quantity3+thenum1;
//                                    }
//                                    avblbalance=avblbalance-quantity3;
//                                    float thenum3 = MapUtils.getFloat(map, "thenum");//调拨数量
//                                    if (avblbalance.compareTo(thenum3)<0){
//                                        int y = i + 1;
//                                        msg = "调拨物料第" + y + "条数据中物料现有可用余量为" + avblbalance + ",与该调拨表中可用余量不等，请重新选择物料";
//                                    }
//                                }else if (CollectionUtils.isEmpty(bySql4)){
//                                    float thenum3 = MapUtils.getFloat(map, "thenum");//调拨数量
//                                    if (avblbalance.compareTo(thenum3)<0){
//                                        int y = i + 1;
//                                        msg = "调拨物料第" + y + "条数据中物料现有可用余量为" + avblbalance + ",与该调拨表中可用余量不等，请重新选择物料";
//                                    }
//                                }
//                            }
                                if (avblbalance.compareTo(thenum) < 0) {
                                    int y = i + 1;
                                    float avblbalance1=avblbalance-thenum2;
                                    msg = "调拨物料第" + y + "条数据中物料现有可用余量为" + avblbalance1 + ",与该调拨表中可用余量不等，请重新选择物料";
                                }





                    }
                } else {
                    return "请添加调拨物料列表行数据！";
                }
            }else if (menutype.equals("2")){
                //给子表也加上一个类型，1和2，用类型进行约束
                List<Map<String, Object>> bySql1 = handlerService.findBySql("select * from gq_transferstoitem where transferstoid ='" + transferstoid + "'");
                if (CollectionUtils.isNotEmpty(bySql1)) {
                    for (int i = 0; i < bySql1.size(); i++) {
                        Map<String, Object> map = bySql1.get(i);
                        String id1 = MapUtils.getString(map, "id");
                        String itemnum = MapUtils.getString(map, "itemnum");
                        String fromlot = MapUtils.getString(map, "fromlot");//原批次
                        Float orderqty = MapUtils.getFloat(map, "orderqty");//申请数量
                        float thenum = MapUtils.getFloat(map, "thenum");//调拨数量
                        Float inventory = MapUtils.getFloat(map, "Inventory");//可用余量
                        String outprice = MapUtils.getString(map, "outprice");
                        BigDecimal bigDecimal = new BigDecimal(outprice);
                        BigDecimal bigDecimal1 = new BigDecimal(orderqty);
                        BigDecimal outcost = new BigDecimal("0");
                        outcost=bigDecimal.multiply(bigDecimal1);
                        handlerService.updateBySql("update gq_transferstoitem set thenum="+orderqty+",outcost="+outcost+" where id='"+id1+"'");
//                        float thenum2 = 0;
//                        List<Map<String, Object>> bySql7 = handlerService.findBySql("select t.* from gq_transferstoitem t left join gq_transfersto a on t.transferstoid=a.id where a.outwarehousenum='"+outwarehousenum+"' and t.itemnum ='" + itemnum + "' and  t.fromlot ='" + fromlot + "' and a.status!='新建' and a.status!='已作废' and a.status!='审批通过'");
//                        if (CollectionUtils.isNotEmpty(bySql7)) {
//                            for (int j = 0; j < bySql7.size(); j++) {
//                                Map<String, Object> map1 = bySql7.get(j);
//                                Float thenum1 = MapUtils.getFloat(map1, "thenum");
//                                thenum2 = thenum1 + thenum2;
//                            }
//                            thenum=thenum+thenum2;
//                        }
//                            List<Map<String, Object>> bySql3 = handlerService.findBySql("select * from gq_invbalances where itemnum='" + itemnum + "' and batchnum='" + fromlot + "' and subjectno = '" + primarysubjectno + "' and storenum = '" + outwarehousenum + "'");
//                            Float avblbalance = (float) 0;
//                            if (CollectionUtils.isNotEmpty(bySql3)) {
//                                Map<String, Object> map1 = bySql3.get(0);
//                                avblbalance = MapUtils.getFloat(map1, "avblbalance");
//                            }
//
//                                if (avblbalance.compareTo(thenum) < 0) {
//                                    int y = i + 1;
//                                    float avblbalance1=avblbalance-thenum2;
//                                    msg = "领料物料第" + y + "条数据中物料现有可用余量为" + avblbalance1 + ",与该领料表中可用余量不等，请重新选择物料";
//                                }



                    }
                } else {
                    return "请添加调拨物料列表行数据！";
                }
            }
        }

        //库管员审批
        if (flowNode.getId().equals("outmanager")){
            List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_transfersto where id ="+id);
            if (CollectionUtils.isNotEmpty(bySql)){
                String menutype = MapUtils.getString(bySql.get(0), "menutype");
                String transferstoid = MapUtils.getString(bySql.get(0), "id");
                String outwarehousenum =MapUtils.getString(bySql.get(0),"outwarehousenum");//调出库房编码
                String primarysubjectno =MapUtils.getString(bySql.get(0),"primarysubjectno");//课题号
                String inwarehousenum = MapUtils.getString(bySql.get(0), "inwarehousenum");//调入库房
                String transferapplicationtype = MapUtils.getString(bySql.get(0), "transferapplicationtype");//类型
                if (menutype.equals("2")){
                    //给子表也加上一个类型，1和2，用类型进行约束
                    List<Map<String, Object>> bySql1 = handlerService.findBySql("select * from gq_transferstoitem where transferstoid ='" + transferstoid + "'");
                    if (CollectionUtils.isNotEmpty(bySql1)) {
                        for (int i = 0; i < bySql1.size(); i++) {
                            Map<String, Object> map = bySql1.get(i);
                            String id1 = MapUtils.getString(map, "id");
                            String itemnum = MapUtils.getString(map, "itemnum");
                            String fromlot = MapUtils.getString(map, "fromlot");//原批次
                            Float orderqty = MapUtils.getFloat(map, "orderqty");//申请数量
                            float thenum = MapUtils.getFloat(map, "thenum");//调拨数量
                            Float inventory = MapUtils.getFloat(map, "Inventory");//可用余量
                            float thenum2 = 0;
                            List<Map<String, Object>> bySql7 = handlerService.findBySql("select t.* from gq_transferstoitem t left join gq_transfersto a on t.transferstoid=a.id where a.outwarehousenum='"+outwarehousenum+"' and t.itemnum ='" + itemnum + "' and  t.fromlot ='" + fromlot + "' and a.status!='新建' and a.status!='已作废' and a.status!='审批通过' and a.id!='"+transferstoid+"'");
                            if (CollectionUtils.isNotEmpty(bySql7)) {
                                for (int j = 0; j < bySql7.size(); j++) {
                                    Map<String, Object> map1 = bySql7.get(j);
                                    Float thenum1 = MapUtils.getFloat(map1, "thenum");
                                    thenum2 = thenum1 + thenum2;
                                }
                                thenum=thenum+thenum2;
                            }
                            List<Map<String, Object>> bySql3 = handlerService.findBySql("select * from gq_invbalances where itemnum='" + itemnum + "' and batchnum='" + fromlot + "' and subjectno = '" + primarysubjectno + "' and storenum = '" + outwarehousenum + "'");
                            Float avblbalance = (float) 0;
                            if (CollectionUtils.isNotEmpty(bySql3)) {
                                Map<String, Object> map1 = bySql3.get(0);
                                avblbalance = MapUtils.getFloat(map1, "avblbalance");
                            }

                            if (avblbalance.compareTo(thenum) < 0) {
                                int y = i + 1;
                                float avblbalance1=avblbalance-thenum2;
                                msg = "领料物料第" + y + "条数据中物料现有可用余量为" + avblbalance1 + ",与该领料表中可用余量不等，请重新选择物料";
                            }



                        }
                    }
                }
            }

        }
        if (flowNode.getId().equals("approver")) {
            /**
             * 当移库单状态变更为“完成”时需要验证该移库单明细中所选物资是否在原仓库是否存在，
             * 且相同库位、相同批次的库存余量大于移库数量。如果验证不通过则抛出提示。
             */
            List<Map<String, Object>> getTransferstonumById = handlerService.findBySql("select transferstonum,outwarehousenum from gq_transfersto where id = '" + id + "'");
            if (CollectionUtils.isNotEmpty(getTransferstonumById)) {
                Map<String, Object> transferstonumMap = getTransferstonumById.get(0);
                String transferstonum = MapUtils.getString(transferstonumMap, "transferstonum");
                String outwarehousenum = MapUtils.getString(transferstonumMap, "outwarehousenum");
                String transferstoid = MapUtils.getString(transferstonumMap, "transferstoid");
                if (StringUtils.isNotBlank(transferstonum)) {
                    List<Map<String, Object>> getTransferstoitemList = handlerService.findBySql("select * from gq_transferstoitem where transferstoid = '" + transferstoid + "'");
                    StringJoiner stringJoinerTtemdesc = new StringJoiner(",");
                    StringJoiner stringJoinerThenum = new StringJoiner(",");
                    if (CollectionUtils.isNotEmpty(getTransferstoitemList)) {
                        for (int i = 0; i < getTransferstoitemList.size(); i++) {
                            Map<String, Object> TransferstoitemMap = getTransferstoitemList.get(i);
                            //判断移库单明细中所选物资是否在原仓库是否存在
                            String itemnum = MapUtils.getString(TransferstoitemMap, "itemnum");
                            String itemdesc = MapUtils.getString(TransferstoitemMap, "itemdesc");
                            List<Map<String, Object>> getInventoryCount = handlerService.findBySql("select id from gq_inventory where storenum = '" + outwarehousenum + "' and itemnum = '" + itemnum + "'");
                            if (CollectionUtils.isEmpty(getInventoryCount)) {
                                stringJoinerTtemdesc.add(itemdesc);
                            }
                            //判断相同库位相同批次的库存余量是否大于移库数量
                            String frombin = MapUtils.getString(TransferstoitemMap, "frombin");
                            String fromlot = MapUtils.getString(TransferstoitemMap, "fromlot");
                            Integer thenum = MapUtils.getInteger(TransferstoitemMap, "thenum");
                            Integer curbal = transferstoDao.getCurbal(frombin, fromlot, outwarehousenum, itemnum);
                            if (thenum > curbal) {
                                stringJoinerThenum.add(itemdesc);
                            }
                        }
                    }
                    if (stringJoinerTtemdesc.length() != 0) {
                        msg="移库单明细中所选物资"+ stringJoinerTtemdesc +"在原仓库中不存在";
                        //throw new Exception("移库单明细中所选物资"+ stringJoinerTtemdesc +"在原仓库中不存在");
                    }
                    if (stringJoinerThenum.length() != 0) {
                        msg="移库单明细中所选物资" + stringJoinerThenum + "移库数量大于库存余量";
                        //throw new Exception("移库单明细中所选物资" + stringJoinerThenum + "移库数量大于库存余量");
                    }
                }
            }
        }
        if(flowNode.getId().equals("outmanager")){
            List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_transferstoitem where transferstoid='" + id + "'");
            if(CollectionUtils.isNotEmpty(bySql)){
                for (int i=0;i<bySql.size();i++){
                    Map<String, Object> map = bySql.get(i);
                    String tobin = MapUtils.getString(map, "tobin");
                    String itemnum = MapUtils.getString(map, "itemnum");
                    if (StringUtils.isEmpty(tobin)){
                        int y=i+1;
                        return "第"+y+"行物料为"+itemnum+"的调拨后货位未填写，请检查！";
                    }
                }            }
        }
        return msg;
    }

    @Override
    public void afterMoveNode(String app, Long id) throws Exception {
        if ("transferstoApp".equals(app)){
            List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_transfersto where id='" + id + "'");
            if (CollectionUtils.isNotEmpty(bySql)){
                List<Map<String, Object>> bySql1 = handlerService.findBySql("select * from gq_transferstoitem where transferstoid='" + id + "'");
                if (CollectionUtils.isNotEmpty(bySql1)){
                    String tobin="";
                    handlerService.updateBySql("update gq_transferstoitem set tobin='"+tobin+"' where transferstoid='"+id+"'");
                }
            }
        }
        super.afterMoveNode(app, id);
    }

    @Override
    public void afterExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op, String event) throws Exception {
        if(flowNode.getId().equals("inleader")){
            boolean flag =op.isFlag();
            if(flag){
              /*1.获取调拨单调拨编号,根据调拨编号查询子表物料信息
                2.判断该调拨信息是调拨还是转移
                2.1是调拨的话，原仓库进行出库操作，目标仓库进行入库操作。
                2.2是转移的话，审批通过修改库存余量表里需求人为目标需求人
                2.循环子表物料,获取物料编号
                3.获取调拨单调入仓库编号,根据调入仓库编号和物料编号 判断库存台账是否存在
                3.1 若存在,获取调拨子表库位信息, 判断调入仓库子表调入库位是否存在物料,
                3.1.1 若存在,直接添加数量
                3.1.2 若不存在,新建库存余量信息
                3.2若不存在,新建库存台账信息
                3.2.1 新建库存余量信息
                3.2.2.新建库存成本信息
                4.根据库存余量子表avblbalance 可用余量  curbal 库存余量 更新主表 curbaltotal,avblbalance
                5.新建调拨记录信息*/

              //1.获取调拨单调拨编号,根据调拨编号查询子表物料信息
                List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_transfersto where id ="+id);
                String transferstonum =MapUtils.getString(bySql.get(0),"transferstonum");
                String transferstodesc =MapUtils.getString(bySql.get(0),"transferstodesc");
                String transferstoid = MapUtils.getString(bySql.get(0), "id");
                String itemnum2 ="";
                String itemdesc2 ="";
                String inwarehousenum =MapUtils.getString(bySql.get(0),"inwarehousenum");//目标仓库编号
                String inwarehousedesc = MapUtils.getString(bySql.get(0), "inwarehousedesc");//目标仓库描述
                String outwarehousenum =MapUtils.getString(bySql.get(0),"outwarehousenum");//原仓库编号
                String transferapplicationtype =MapUtils.getString(bySql.get(0),"transferapplicationtype");//类型01为调拨，02为转移
                String targetdemandid =MapUtils.getString(bySql.get(0),"targetdemandid");//目标需求人id
                String targetdemand = MapUtils.getString(bySql.get(0), "targetdemand");//需求人名称
                String outwarehousedesc =MapUtils.getString(bySql.get(0),"outwarehousedesc");//原仓库描述
                String primarysubjectno =MapUtils.getString(bySql.get(0),"primarysubjectno");//原课题号
                String proid = MapUtils.getString(bySql.get(0), "proid");//原项目id
                String proid1 = MapUtils.getString(bySql.get(0), "proid1");//新项目id
                String targetsubjectno = MapUtils.getString(bySql.get(0), "targetsubjectno");//新课题号
                String menutype =MapUtils.getString(bySql.get(0),"menutype");//1为调拨申请2为部门领用
                List<Map<String, Object>> bySql1 = handlerService.findBySql("select * from gq_transferstoitem where transferstoid ='" + transferstoid + "'");
              //2.循环子表物料,获取物料编号
                for(Map<String, Object> map :bySql1){
                    //3.获取调拨单调入仓库编号,根据调入仓库编号和物料编号 判断库存台账是否存在
                    String itemnum =MapUtils.getString(map,"itemnum");
                    itemnum2 =itemnum;
                    String itemdesc =MapUtils.getString(map,"itemdesc");
                    itemdesc2 =itemdesc;
                    String fromlot = MapUtils.getString(map,"fromlot");//原批次
                    String frombin = MapUtils.getString(map, "frombin");//原货位
                    String tobin = MapUtils.getString(map,"tobin");//调拨后货位
                    Float thenum =MapUtils.getFloat(map,"thenum");//调拨数量
                    String outprice1 =MapUtils.getString(map,"outprice");
                    BigDecimal outprice = new BigDecimal(outprice1);
                    String glid = MapUtils.getString(map,"glid");//库存余量里id
                    String createuser = MapUtils.getString(map, "createuser");
                    String deptcode = MapUtils.getString(map, "deptcode");
                    String unit = MapUtils.getString(map, "unit");
                    String subjectno = MapUtils.getString(map, "subjectno");
                    String proid2 = MapUtils.getString(map, "proid");
                    if("转移".equals(transferapplicationtype)){
                        //2.2是转移的话，审批通过是物料进行货位间的调拨
                        List<Map<String, Object>> bySql2 = handlerService.findBySql("select * from gq_invbalances where itemnum='" + itemnum + "' and storenum='" + outwarehousenum + "' and batchnum='" + fromlot + "' and  subjectno='" + primarysubjectno + "' and binnum='"+frombin+"'");
                        if (CollectionUtils.isNotEmpty(bySql2)){
                            Map<String, Object> map1 = bySql2.get(0);
                            String binnum = MapUtils.getString(map1, "binnum");//货位
                            String batchnum = MapUtils.getString(map1, "batchnum");//批次
                            Float avblbalance = MapUtils.getFloat(map1, "avblbalance");//可用数量
                            Float curbal = MapUtils.getFloat(map1, "curbal");//库存余量
                            Float reservedqty = MapUtils.getFloat(map1, "reservedqty");//预留数量
                            String proid3 = MapUtils.getString(map1, "proid");//项目编号
                            String taskid = MapUtils.getString(map1, "taskid");//任务编号
                            String subjectno1 = MapUtils.getString(map1, "subjectno");//课题号
                            String adjusteduser = MapUtils.getString(map1, "adjusteduser");//调整人
                            String enddate = MapUtils.getString(map1, "enddate");//失效日期
                            String startdate = MapUtils.getString(map1, "startdate");//入库时间
                            String adjustdesc = MapUtils.getString(map1, "adjustdesc");//调整说明
                            String applyby = MapUtils.getString(map1, "applyby");//占用人
                            String manbanumber = MapUtils.getString(map1, "manbanumber");//厂商批次号
                            String aactualdate = MapUtils.getString(map1, "aactualdate");//出厂日期
                            String receiptnum = MapUtils.getString(map1, "receiptnum");//采购接收编号

                            //调拨前货位
                            avblbalance=avblbalance-thenum;
                            curbal=curbal-thenum;
                            reservedqty=curbal-avblbalance;
                            handlerService.updateBySql("update gq_invbalances set avblbalance="+avblbalance+",curbal="+curbal+",reservedqty="+reservedqty+" where itemnum='" + itemnum + "' and storenum='" + outwarehousenum + "' and batchnum='" + fromlot + "' and  subjectno='" + primarysubjectno + "' and binnum='" + binnum + "'");
                            List<Map<String, Object>> bySql3 = handlerService.findBySql("select * from gq_invbalances where itemnum='" + itemnum + "' and storenum='" + outwarehousenum + "' and batchnum='" + fromlot + "' and  subjectno='" + primarysubjectno + "' and binnum='" + tobin + "' and batchnum='" + fromlot + "'");
                            if (CollectionUtils.isNotEmpty(bySql3)){
                                Map<String, Object> map2 = bySql3.get(0);
                                //调拨后货位
                                Float avblbalance1 = MapUtils.getFloat(map2, "avblbalance");
                                Float curbal1 = MapUtils.getFloat(map2, "curbal");
                                Float reservedqty1 = MapUtils.getFloat(map2, "reservedqty");
                                avblbalance1=avblbalance1+thenum;
                                curbal1=curbal1+thenum;
                                reservedqty1=curbal1-avblbalance1;
                                handlerService.updateBySql("update gq_invbalances set avblbalance="+avblbalance1+",curbal="+curbal1+",reservedqty="+reservedqty1+" where itemnum='" + itemnum + "' and storenum='" + outwarehousenum + "' and batchnum='" + fromlot + "' and  subjectno='" + primarysubjectno + "' and binnum='" + tobin + "'");
                            }else {
                                HashMap<String, Object> hashMap = new HashMap<>();
                                hashMap.put("binnum",tobin);
                                hashMap.put("batchnum",batchnum);
                                hashMap.put("avblbalance",thenum);
                                hashMap.put("curbal",thenum);
                                hashMap.put("reservedqty",0);
                                hashMap.put("proid",proid3);
                                hashMap.put("taskid",taskid);
                                hashMap.put("subjectno",subjectno1);
                                hashMap.put("adjusteduser",adjusteduser);
                                hashMap.put("enddate",enddate);
                                hashMap.put("unitcost",outprice);//单价
                                hashMap.put("startdate",startdate);
                                hashMap.put("adjustdesc",adjustdesc);
                                hashMap.put("applyby",applyby);
                                hashMap.put("manbanumber",manbanumber);
                                hashMap.put("aactualdate",aactualdate);
                                hashMap.put("receiptnum",receiptnum);
                                hashMap.put("itemnum",itemnum);
                                hashMap.put("itemdesc",itemdesc);
                                hashMap.put("orderunit",unit);
                                hashMap.put("storenum",outwarehousenum);
                                handlerService.insert("gq_invbalances", hashMap);
                            }
                            LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser();
                            Long userId = loginAppUser.getId();
                            BigDecimal linecost = new BigDecimal("0");
                            BigDecimal thenum1 = new BigDecimal(thenum);
                            linecost =thenum1.multiply(outprice);
                            //修改了生成出入库记录编号是一样的，还有没有把库房信息放进去
                            String instonumNew="";//入库编号
                            String outonumNew="";//出库编号
                            List<Map<String, Object>> bySql4 = handlerService.findBySql("SELECT instonum from gq_matusetrans where instonum  LIKE '%CDFI%' order by instonum desc limit 1");
                            List<Map<String, Object>> bySql5 = handlerService.findBySql("SELECT instonum from gq_matusetrans where instonum  LIKE '%CDFO%' order by instonum desc limit 1");
                            if(CollectionUtils.isNotEmpty(bySql4)){
                                Map<String, Object> map5 = bySql4.get(0);
                                String instonum = MapUtils.getString(map5, "instonum");
                                int integer = Integer.valueOf(instonum.substring(instonum.length() - 8));
                                integer++;
                                instonumNew="CDFI"+integer;
                            }else{
                                instonumNew="CDFI10000001";
                            }
                            if(CollectionUtils.isNotEmpty(bySql5)){
                                Map<String, Object> map6 = bySql5.get(0);
                                String instonum = MapUtils.getString(map6, "instonum");
                                int integer = Integer.valueOf(instonum.substring(instonum.length() - 8));
                                integer++;
                                outonumNew="CDFO"+integer;
                            }else{
                                outonumNew="CDFO10000001";
                            }
                            handlerService.updateBySql("insert into gq_matusetrans (subjectno,binnum,storenum,storedesc,instonum,instodesc,itemnum,itemdesc,outwarehousenum,inwarehousenum,sublessordesc,createuser,createtime,status,type,quantity,unitcost,linecost,deptcode,lotnum,unit) values('"+subjectno+"','"+tobin+"','"+outwarehousenum+"','"+outwarehousedesc+"','"+instonumNew+"','"+transferstodesc+"','"+itemnum2+"','"+itemdesc2+"','"+outwarehousenum+"','"+outwarehousenum+"','"+targetdemand+"','"+userId+"',now(),'已完成','调入','"+thenum+"','"+outprice+"','"+linecost+"','"+deptcode+"','"+fromlot+"','"+unit+"')");
                            handlerService.updateBySql("insert into gq_matusetrans (subjectno,binnum,storenum,storedesc,instonum,instodesc,itemnum,itemdesc,outwarehousenum,inwarehousenum,sublessordesc,createuser,createtime,status,type,quantity,unitcost,linecost,deptcode,lotnum,unit,receiptnum) values('"+subjectno+"','"+binnum+"','"+outwarehousenum+"','"+outwarehousedesc+"','" + outonumNew + "','" + transferstodesc + "','" + itemnum2 + "','" + itemdesc2 + "','" + outwarehousenum + "','" + outwarehousenum + "','" + targetdemand + "','" + userId + "',now(),'已完成','调出','" + thenum + "','" + outprice + "','" + linecost + "','"+deptcode+"','"+fromlot+"','"+unit+"','"+transferstonum+"')");
                        }
                    }else{
                        //2.1是调拨的话走之前的逻辑调拨逻辑
                        List<Map<String, Object>> bySql2 = handlerService.findBySql("select * from gq_inventory where itemnum ='" + itemnum + "' and storenum ='" + inwarehousenum + "'");
                        //3.1若不存在,新建库存台账,以及成本,余量子表
                        if(bySql2 ==null || bySql2.size()==0){
                            List<Map<String, Object>> bySql3 = handlerService.findBySql("select * from gq_inventory where itemnum ='" + itemnum + "' and storenum ='" + outwarehousenum + "'");
                            Map<String, Object> map1 =bySql3.get(0);
                            map1.remove("id");
                            map1.remove("storenum");
                            map1.put("storenum",inwarehousenum);
                            map1.put("storedesc",inwarehousedesc);
                            map1.remove("reservedqty");//入库的预留余量不需要赋值
                            handlerService.insert("gq_inventory",map1);
                            List<Map<String, Object>> bySql4 = handlerService.findBySql("select * from gq_invcost where itemnum ='" + itemnum + "' and storenum ='" + outwarehousenum + "' and batchnum ='"+fromlot+"'");
                            for(Map<String, Object> map2 :bySql4){

                                map2.remove("id");
                                if (StringUtils.isNotEmpty(targetsubjectno)){
                                    map2.remove("subjectno");
                                    map2.put("subjectno",targetsubjectno);
                                    map2.remove("proid");
                                    map2.put("proid",proid1);
                                }
                                map2.remove("storenum");
                                map2.put("storenum",inwarehousenum);
                                map2.remove("batchnum");
                                map2.put("batchnum",fromlot);
                                handlerService.insert("gq_invcost",map2);
                            }
                            List<Map<String, Object>> bySql5 = handlerService.findBySql("select * from gq_invbalances where itemnum ='" + itemnum + "' and storenum ='" + outwarehousenum + "' and batchnum ='"+fromlot+"' and binnum='" + frombin + "'");
                            for(Map<String, Object> map3 :bySql5){
                                map3.remove("id");
                                if (StringUtils.isNotEmpty(targetsubjectno)){
                                    map3.remove("subjectno");
                                    map3.put("subjectno",targetsubjectno);
                                    map3.remove("proid");
                                    map3.put("proid",proid1);
                                }
                                map3.remove("storenum");
                                map3.put("storenum",inwarehousenum);
                                map3.remove("batchnum");
                                map3.put("batchnum",fromlot);
                                map3.remove("binnum");
                                map3.put("binnum",tobin);
                                map3.remove("avblbalance");
                                map3.put("avblbalance",thenum);
                                map3.remove("curbal");
                                map3.put("curbal",thenum);
                                map3.remove("reservedqty");
                                //去掉采购接收编号
                                map3.remove("receiptnum");
                                map3.put("receiptnum","0");
                                handlerService.insert("gq_invbalances",map3);
                            }
                        }else {
                            String sql="";
                            if (StringUtils.isNotEmpty(targetsubjectno)){
                                 sql="and subjectno='"+targetsubjectno+"' and proid='"+proid1+"'";
                            }
                            //3.1 若存在,获取调拨子表库位信息, 判断调入仓库子表调入库位是否存在物料,
                            List<Map<String, Object>> bySql3=null;
                            if(menutype.equals("1")){
                                bySql3= handlerService.findBySql("select * from gq_invbalances where itemnum ='"+itemnum+"' and storenum ='"+inwarehousenum+"' and binnum='" + tobin + "' and batchnum ='"+fromlot+"'"+sql);
                            }else if (menutype.equals("2")){
                                bySql3= handlerService.findBySql("select * from gq_invbalances where itemnum ='"+itemnum+"' and storenum ='"+inwarehousenum+"' and binnum='" + tobin + "' and batchnum ='"+fromlot+"' and subjectno='"+subjectno+"' and proid='"+proid2+"'");
                            }
                            if(bySql3 !=null && bySql3.size() !=0){
                                //3.1.1 若存在,直接添加数量
                                //avblbalance 可用余量  curbal 库存余量
                                Float avblbalance =MapUtils.getFloat(bySql3.get(0),"avblbalance");
                                Float curbal =MapUtils.getFloat(bySql3.get(0),"curbal");
                                avblbalance =avblbalance +thenum;
                                curbal =curbal +thenum;
                                if(menutype.equals("1")){
                                    handlerService.updateBySql("update gq_invbalances set avblbalance ="+avblbalance+",curbal ="+curbal +" where itemnum ='"+itemnum+"' and storenum ='"+inwarehousenum+"' and binnum='" + tobin + "' and batchnum ='"+fromlot+"'"+sql);
                                }else if (menutype.equals("2")){
                                    handlerService.updateBySql("update gq_invbalances set avblbalance ="+avblbalance+",curbal ="+curbal +" where itemnum ='"+itemnum+"' and storenum ='"+inwarehousenum+"' and binnum='" + tobin + "' and batchnum ='"+fromlot+"' and subjectno='"+subjectno+"' and proid='"+proid2+"'");

                                }

                            }else{
                                List<Map<String, Object>> bySql5 = handlerService.findBySql("select * from gq_invbalances where itemnum ='" + itemnum + "' and storenum ='" + outwarehousenum + "' and binnum='" + frombin + "' and batchnum ='"+fromlot+"' and subjectno='"+subjectno+"' and proid='"+proid2+"'");
                                for(Map<String, Object> map3 :bySql5){
                                    map3.remove("id");
                                    if (StringUtils.isNotEmpty(targetsubjectno)){
                                        map3.remove("subjectno");
                                        map3.put("subjectno",targetsubjectno);
                                        map3.remove("proid");
                                        map3.put("proid",proid1);
                                    }
                                    map3.remove("storenum");
                                    map3.put("storenum",inwarehousenum);
                                    map3.remove("batchnum");
                                    map3.put("batchnum",fromlot);
                                    map3.remove("binnum");
                                    map3.put("binnum",tobin);
                                    map3.remove("avblbalance");
                                    map3.put("avblbalance",thenum);
                                    map3.remove("curbal");
                                    map3.put("curbal",thenum);
                                    //去掉采购接收编号
                                    map3.remove("receiptnum");
                                    map3.put("receiptnum","0");
                                    handlerService.insert("gq_invbalances",map3);
                                }
                            }
                            //TODO 为什么目标课题号为null  就不会往库存台账里的库存成本表插数据
                            //判断库存成本
                            String sql1="";
                            if (StringUtils.isNotEmpty(targetsubjectno)){
                                sql1="and subjectno='"+targetsubjectno+"' and proid='"+proid1+"'";
                                List<Map<String, Object>> bySql14 = handlerService.findBySql("select * from gq_invcost where itemnum ='"+itemnum+"' and storenum ='"+inwarehousenum+"' and batchnum ='"+fromlot+"'"+sql1);
                                if(CollectionUtils.isNotEmpty(bySql14)){

                                }else {
                                    List<Map<String, Object>> bySql15 = handlerService.findBySql("select * from gq_invcost where itemnum ='" + itemnum + "' and storenum ='" + outwarehousenum + "' and batchnum ='"+fromlot+"' and subjectno='"+subjectno+"' and proid='"+proid2+"'");
                                    for (int z=0;z<bySql15.size();z++){
                                        Map<String, Object> map1 = bySql15.get(z);
                                        map1.remove("id");
                                        if (StringUtils.isNotEmpty(targetsubjectno)){
                                            map1.remove("subjectno");
                                            map1.put("subjectno",targetsubjectno);
                                            map1.remove("proid");
                                            map1.put("proid",proid1);
                                            map1.remove("storenum");
                                            map1.put("storenum",inwarehousenum);
                                            map1.remove("batchnum");
                                            map1.put("batchnum",fromlot);
                                        }
                                        handlerService.insert("gq_invcost",map1);
                                    }

                                }

                            }else {
                                sql1="and subjectno='"+subjectno+"' and proid='"+proid2+"'";
                                List<Map<String, Object>> bySql14 = handlerService.findBySql("select * from gq_invcost where itemnum ='"+itemnum+"' and storenum ='"+inwarehousenum+"' and batchnum ='"+fromlot+"'"+sql1);
                                if(CollectionUtils.isNotEmpty(bySql14)){

                                }else {
                                    List<Map<String, Object>> bySql15 = handlerService.findBySql("select * from gq_invcost where itemnum ='" + itemnum + "' and storenum ='" + outwarehousenum + "' and batchnum ='"+fromlot+"' and subjectno='"+subjectno+"' and proid='"+proid2+"'");
                                    for (int z=0;z<bySql15.size();z++){
                                        Map<String, Object> map1 = bySql15.get(z);
                                        map1.remove("id");
                                            map1.remove("subjectno");
                                            map1.put("subjectno",subjectno);
                                            map1.remove("proid");
                                            map1.put("proid",proid2);
                                            map1.remove("storenum");
                                            map1.put("storenum",inwarehousenum);
                                            map1.remove("batchnum");
                                            map1.put("batchnum",fromlot);
                                        handlerService.insert("gq_invcost",map1);
                                    }

                                }
                            }
                        }
                        List<Map<String, Object>> bySql3 = handlerService.findBySql("select avblbalance,curbal from gq_invbalances where itemnum ='" + itemnum2 + "' and storenum ='" + outwarehousenum + "' and batchnum ='"+fromlot+"' and binnum='" + frombin + "'");
                        for(Map<String, Object> map1 :bySql3){
                            Float avblbalance = MapUtils.getFloat(map1,"avblbalance")-thenum;
                            Float curbal = MapUtils.getFloat(map1,"curbal")-thenum;
                            handlerService.updateBySql("update gq_invbalances set curbal ="+curbal+",avblbalance ="+avblbalance +" where itemnum ='" + itemnum2 + "' and storenum ='" + outwarehousenum + "' and batchnum ='"+fromlot+"' and binnum='" + frombin + "'");
                        }
                        //4.根据库存余量子表avblbalance 可用余量  curbal 库存余量 更新主表 curbaltotal,avblbalance
                        List<Map<String, Object>> bySql10 = handlerService.findBySql("select avblbalance,curbal from gq_invbalances where itemnum ='" + itemnum2 + "' and storenum ='" + inwarehousenum + "'");
                        Integer curbaltotal =0;
                        Integer avblbalancetotal =0;
                        for(Map<String, Object> map10 :bySql10){
                            Integer avblbalance = MapUtils.getInteger(map10,"avblbalance");
                            Integer curbal = MapUtils.getInteger(map10,"curbal");
                            curbaltotal =curbaltotal +curbal;
                            avblbalancetotal =avblbalancetotal +avblbalance;
                        }
                        handlerService.updateBySql("update gq_inventory set curbaltotal ="+curbaltotal+",avblbalance ="+avblbalancetotal +" where itemnum ='" + itemnum2 + "' and storenum ='" + inwarehousenum + "'");
                        List<Map<String, Object>> bySql11 = handlerService.findBySql("select avblbalance,curbal from gq_invbalances where itemnum ='" + itemnum2 + "' and storenum ='" + outwarehousenum + "'");
                        Float curbaltotal3 =(float)0;
                        Float avblbalancetotal3 =(float)0;
                        for(Map<String, Object> map11 :bySql11){
                            Float avblbalance3 = MapUtils.getFloat(map11,"avblbalance");
                            Float curbal3 = MapUtils.getFloat(map11,"curbal");
                            curbaltotal3 =curbaltotal3 +curbal3;
                            avblbalancetotal3 =avblbalancetotal3 +avblbalance3;
                        }
                        handlerService.updateBySql("update gq_inventory set curbaltotal ="+curbaltotal3+",avblbalance ="+avblbalancetotal3 +" where itemnum ='" + itemnum2 + "' and storenum ='" + outwarehousenum + "'");

                        //5.新建调拨记录信息
                        LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser();
                        Long userId = loginAppUser.getId();
                        String sublessordesc =loginAppUser.getCreateuserdesc();
                        BigDecimal linecost = new BigDecimal("0");
                        BigDecimal thenum1 = new BigDecimal(thenum);
                        linecost =thenum1.multiply(outprice);
                        //修改了生成出入库记录编号是一样的，还有没有把库房信息放进去
                        String instonumNew="";//入库编号
                        String outonumNew="";//出库编号
                        List<Map<String, Object>> bySql4 = handlerService.findBySql("SELECT instonum from gq_matusetrans where instonum  LIKE '%CDFI%' order by instonum desc limit 1");
                        List<Map<String, Object>> bySql5 = handlerService.findBySql("SELECT instonum from gq_matusetrans where instonum  LIKE '%CDFO%' order by instonum desc limit 1");
                        if(CollectionUtils.isNotEmpty(bySql4)){
                            Map<String, Object> map1 = bySql4.get(0);
                            String instonum = MapUtils.getString(map1, "instonum");
                            int integer = Integer.valueOf(instonum.substring(instonum.length() - 8));
                            integer++;
                            instonumNew="CDFI"+integer;
                        }else{
                            instonumNew="CDFI10000001";
                        }
                        if(CollectionUtils.isNotEmpty(bySql5)){
                            Map<String, Object> map1 = bySql5.get(0);
                            String instonum = MapUtils.getString(map1, "instonum");
                            int integer = Integer.valueOf(instonum.substring(instonum.length() - 8));
                            integer++;
                            outonumNew="CDFO"+integer;
                        }else{
                            outonumNew="CDFO10000001";
                        }
                        if("1".equals(menutype)){
                            handlerService.updateBySql("insert into gq_matusetrans (subjectno,binnum,storenum,storedesc,instonum,instodesc,itemnum,itemdesc,outwarehousenum,inwarehousenum,sublessordesc,createuser,createtime,status,type,quantity,unitcost,linecost,deptcode,lotnum,unit) values('"+subjectno+"','"+tobin+"','"+inwarehousenum+"','"+inwarehousedesc+"','"+instonumNew+"','"+transferstodesc+"','"+itemnum2+"','"+itemdesc2+"','"+outwarehousenum+"','"+inwarehousenum+"','"+targetdemand+"','"+userId+"',now(),'已完成','调入','"+thenum+"','"+outprice+"','"+linecost+"','"+deptcode+"','"+fromlot+"','"+unit+"')");
                            handlerService.updateBySql("insert into gq_matusetrans (subjectno,binnum,storenum,storedesc,instonum,instodesc,itemnum,itemdesc,outwarehousenum,inwarehousenum,sublessordesc,createuser,createtime,status,type,quantity,unitcost,linecost,deptcode,lotnum,unit,receiptnum) values('"+subjectno+"','"+frombin+"','"+outwarehousenum+"','"+outwarehousedesc+"','" + outonumNew + "','" + transferstodesc + "','" + itemnum2 + "','" + itemdesc2 + "','" + outwarehousenum + "','" + inwarehousenum + "','" + targetdemand + "','" + userId + "',now(),'已完成','调出','" + thenum + "','" + outprice + "','" + linecost + "','"+deptcode+"','"+fromlot+"','"+unit+"','"+transferstonum+"')");
                        }else{
                            handlerService.updateBySql("insert into gq_matusetrans (subjectno,binnum,storenum,storedesc,instonum,instodesc,itemnum,itemdesc,outwarehousenum,inwarehousenum,sublessordesc,createuser,createtime,status,type,quantity,unitcost,linecost,deptcode,lotnum,unit) values('"+subjectno+"','"+tobin+"','"+inwarehousenum+"','"+inwarehousedesc+"','"+instonumNew+"','"+transferstodesc+"','"+itemnum2+"','"+itemdesc2+"','"+outwarehousenum+"','"+inwarehousenum+"','"+targetdemand+"','"+userId+"',now(),'已完成','接收','"+thenum+"','"+outprice+"','"+linecost+"','"+deptcode+"','"+fromlot+"','"+unit+"')");
                            handlerService.updateBySql("insert into gq_matusetrans (subjectno,binnum,storenum,storedesc,instonum,instodesc,itemnum,itemdesc,outwarehousenum,inwarehousenum,sublessordesc,createuser,createtime,status,type,quantity,unitcost,linecost,deptcode,lotnum,unit,receiptnum) values('"+subjectno+"','"+frombin+"','"+outwarehousenum+"','"+outwarehousedesc+"','" + outonumNew + "','" + transferstodesc + "','" + itemnum2 + "','" + itemdesc2 + "','" + outwarehousenum + "','" + inwarehousenum + "','" + targetdemand + "','" + userId + "',now(),'已完成','领用出库','" + thenum + "','" + outprice + "','" + linecost + "','"+deptcode+"','"+fromlot+"','"+unit+"','"+transferstonum+"')");
                        }
//                        List<Map<String, Object>> bySql6 = handlerService.findBySql("select avblbalance,version from gq_invbalances where itemnum='" + itemnum + "'and storenum ='" + outwarehousenum + "' and batchnum='" + fromlot + "' ");
////                        if (CollectionUtils.isNotEmpty(bySql6)){
////                            Map<String, Object> map2 = bySql6.get(0);
////                            Float avblbalance1 = MapUtils.getFloat(map2, "avblbalance");
////                            Integer version = MapUtils.getInteger(map2, "version");
////                            version=version+1;
////                            //handlerService.updateBySql("update gq_transferstoitem set Inventory ="+avblbalance1+" where itemnum='" + itemnum + "' and fromlot='" + fromlot + "'");
////                            handlerService.updateBySql("update gq_invbalances set version="+version+" where itemnum='" + itemnum + "'and storenum ='" + outwarehousenum + "' and batchnum='" + fromlot + "'");
////                        }
                    }
                }
            }
        }
    }

    /*    @Override
    public void afterExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op, String event) throws Exception {
        if (flowNode.getId().equals("approver")) {
            //当移库单状态变更为完成时将移库单明细信息写入库存交易表
            List<Map<String, Object>> getTransferstonumById = handlerService.findBySql
                    ("select transferstonum,outwarehousenum,outwarehousedesc,inwarehousenum,inwarehousedesc from gq_transfersto where id = '" + id + "'");
            if (CollectionUtils.isNotEmpty(getTransferstonumById)) {
                Map<String, Object> transferstonumMap = getTransferstonumById.get(0);
                String transferstonum = MapUtils.getString(transferstonumMap, "transferstonum");     //调拨单号
                String outwarehousenum = MapUtils.getString(transferstonumMap, "outwarehousenum");   //转出仓库
                String outwarehousedesc = MapUtils.getString(transferstonumMap, "outwarehousedesc"); //转出仓库描述
                String inwarehousenum = MapUtils.getString(transferstonumMap, "inwarehousenum");     //转入仓库
                String inwarehousedesc = MapUtils.getString(transferstonumMap, "inwarehousedesc");   //转入仓库描述
               String sublessordesc = MapUtils.getString(transferstonumMap, "sublessordesc");     //领用人
                String createtime = MapUtils.getString(transferstonumMap, "createtime");
                if (StringUtils.isNotBlank(transferstonum)) {
                    List<Map<String, Object>> getTransferstoitemList = handlerService.findBySql("select * from gq_transferstoitem where transferstonum = '" + transferstonum + "'");
                    if (CollectionUtils.isNotEmpty(getTransferstoitemList)) {
                        for (int i = 0; i < getTransferstoitemList.size(); i++) {
                            Map<String, Object> TransferstoitemMap = getTransferstoitemList.get(i);
                            String itemnum = MapUtils.getString(TransferstoitemMap, "itemnum");   //物料编号
                            String itemdesc = MapUtils.getString(TransferstoitemMap, "itemdesc"); //物料名称
                            Integer thenum = MapUtils.getInteger(TransferstoitemMap, "thenum");   //数量
                            String frombin = MapUtils.getString(TransferstoitemMap, "frombin");   //库位
                            String fromlot = MapUtils.getString(TransferstoitemMap, "fromlot");   //批次
                            String outprice = MapUtils.getString(TransferstoitemMap, "outprice"); //调拨单价
                            String tobin = MapUtils.getString(TransferstoitemMap, "tobin");//目标库位
                            String tolot = MapUtils.getString(TransferstoitemMap, "tolot");//目标批次
                            String model = MapUtils.getString(TransferstoitemMap, "model");
                            //转出仓库余量扣减， 转入仓库余量增加
                            Map<String, Object> updateInventoryMap = new HashMap<>();
                            updateInventoryMap.put("storenum", outwarehousenum); //转出仓库
                            updateInventoryMap.put("itemnum", itemnum);          //物料
                            updateInventoryMap.put("binnum", frombin);           //库位
                            updateInventoryMap.put("batchnum", fromlot);         //批次
                            updateInventoryMap.put("quantity", thenum);//数量
                            transferstoDao.updateInventory(updateInventoryMap);
                            List<Map<String, Object>> bySql = handlerService.findBySql("SELECT curbal from gq_invbalances WHERE storenum = '" + inwarehousenum + "' and itemnum ='" + itemnum + "' and binnum = '" + tobin + "' and batchnum = '" + tolot + "'");
                            if (CollectionUtils.isNotEmpty(bySql)){
                                for (int j=0;j<0;j++){
                                    Map<String, Object> map = bySql.get(j);
                                    updateInventoryMap.put("storenum", inwarehousenum);  //转入仓库
                                    updateInventoryMap.put("itemnum", itemnum);          //物料
                                    updateInventoryMap.put("binnum",tobin);//目标库位
                                    updateInventoryMap.put("batchnum",tolot);//目标批次
                                    updateInventoryMap.put("quantity", -thenum);         //数量
                                    transferstoDao.updateInventory(updateInventoryMap);
//                                    handlerService.updateBySql();
                                }
                            }
                            *//*else {
                                //更新库存主表
                                HashMap<String, Object> map = new HashMap<>();
                                map.put("storenum",inwarehousenum);
                                map.put("storedesc",inwarehousedesc);
                                map.put("curbaltotal",thenum);
                                map.put("itemnum",itemnum);
                                map.put("itemdesc",itemdesc);
                                map.put("createuserdesc",sublessordesc);
                                map.put("createtime",createtime);
                                map.put("model",model);
                                map.put("avblbalance",thenum);
                                handlerService.insert("gq_inventory",map);
                                //更新库存余量表
                                updateInventoryMap.put("storenum", inwarehousenum);  //转入仓库
                                updateInventoryMap.put("itemnum", itemnum);          //物料
                                updateInventoryMap.put("binnum",tobin);//目标库位
                                updateInventoryMap.put("batchnum",tolot);//目标批次
                                updateInventoryMap.put("quantity", thenum);//数量
                                updateInventoryMap.put("quantit",thenum);
                                transferstoDao.insert(updateInventoryMap);

                            }*//*

                            //修改库存总余量
                            iInventoryService.calInvQuantity(outwarehousenum, itemnum);
                            iInventoryService.calInvQuantity(inwarehousenum, itemnum);

                            Map<String, Object> matusetransMap = new HashMap<>();
                            matusetransMap.put("storenum", outwarehousenum);
                            matusetransMap.put("storedesc", outwarehousedesc);
                            matusetransMap.put("itemnum", itemnum);
                            matusetransMap.put("itemdesc", itemdesc);
                            matusetransMap.put("quantity", thenum);
//                            matusetransMap.put("issueto", recipientsdesc);
                            matusetransMap.put("transdate", new Date());
                            matusetransMap.put("binnum", frombin);
                            matusetransMap.put("lotnum", fromlot);
                            matusetransMap.put("type", "出库");
                            matusetransMap.put("transferstonum", transferstonum);

                            //写入库存交易 ————出库记录
                            handlerService.insert("gq_matusetrans", matusetransMap);

                            //写入库存交易 ————入库记录
                            matusetransMap.put("type", "接收");
                            matusetransMap.put("storenum", inwarehousenum);
                            matusetransMap.put("storedesc", inwarehousedesc);
                            matusetransMap.put("binnum",tobin);
                            matusetransMap.put("batchnum",tolot);
                            handlerService.insert("gq_matusetrans", matusetransMap);

                            //修改库存平均价格  计算公式为（入库数量*入库价格+库存总数量*库存平均价）/入库数量+库存总数量
                            matusetransMap.put("outprice", outprice);
                            iInventoryService.updateInvcost(matusetransMap);
                        }
                    }
                }
            }

//        if(flowNode.getId().equals("drfzr")){
            //审核通过后修改库存预留数量
            boolean flag = op.isFlag();//判断是否审核通过
            //审核通过的情况
            if(flag) {
                List<Map<String, Object>> list = handlerService.findBySql("select * from gq_transfersto where id=" + id);
                String outwarehousenum = MapUtil.getStr(list.get(0), "outwarehousenum");//转出仓库号
                String inwarehousenum = MapUtil.getStr(list.get(0), "inwarehousenum");//转入仓库编码
                String transferstonum = MapUtil.getStr(list.get(0), "transferstonum");//调拨单编码
                List<Map<String, Object>> lineList = handlerService.findBySql("select *from gq_transferstoitem where transferstonum ='" + transferstonum + "'");
                if (lineList != null) {
                    for (int k = 0; k < lineList.size(); k++) {
                        String itemnum = MapUtil.getStr(lineList.get(k), "itemnum");//物料编号
                        Integer thenum = MapUtil.getInt(lineList.get(k), "thenum");//调拨数量
                        // 转出库存
//                        List<Map<String, Object>> Inventorys1 = handlerService.findBySql("select *from bl_transferstoitem where itemnum ='"+itemnum+"' and warehousenum ='"+outwarehousenum+"'");
//                        // 转入库存
//                        List<Map<String, Object>> Inventorys2 = handlerService.findBySql("select *from bl_transferstoitem where itemnum ='"+itemnum+"' and warehousenum ='"+inwarehousenum+"'");
                        // 更新转出库存
                        handlerService.findBySql("update  gq_inventory  set curbaltotal=curbaltotal-" + thenum + ",avblbalance=avblbalance-" + thenum + " where itemnum ='" + itemnum + "' and storenum ='" + outwarehousenum + "'");
                        //更新转入库存
                        handlerService.findBySql("update  gq_inventory  set curbaltotal=curbaltotal+" + thenum + ",avblbalance=avblbalance+" + thenum + " where itemnum ='" + itemnum + "' and storenum ='" + inwarehousenum + "'");
                        //往出库记录插数据
                        Map<String, Object> invuseMap = new HashMap<>();
                        List<Map<String, Object>> bySql = handlerService.findBySql("select invusenum from gq_invuse order by invusenum desc");
                        Integer invusenum = 1000;
                        if (bySql != null && bySql.size() > 0) {
                            Map<String, Object> map = bySql.get(0);
                            invusenum = MapUtils.getInteger(map, "invusenum");
                            invuseMap.put("invusenum", invusenum + 1);
                        } else {
                            invuseMap.put("invusenum", invusenum + 1);
                        }
                        invuseMap.put("description", MapUtil.getStr(list.get(0), "transferstodesc"));
                        invuseMap.put("lldEnitydescription", MapUtil.getStr(list.get(0), "transferstodesc"));
                        invuseMap.put("locations", outwarehousenum);
                        invuseMap.put("deptcode", MapUtil.getStr(list.get(0), "deptcode"));
                        invuseMap.put("orgcode", MapUtil.getStr(list.get(0), "orgcode"));
                        invuseMap.put("status", "已审批");
                        invuseMap.put("createuser", MapUtil.getStr(list.get(0), "createuser"));
                        invuseMap.put("receiveuser", MapUtil.getStr(list.get(0), "createuser"));
                        invuseMap.put("receiveuserdesc", MapUtil.getStr(list.get(0), "createuserdesc"));
                        invuseMap.put("createtime", new Date());
                        handlerService.insert("gq_invuse", invuseMap);
                        //往出库记录里物料列表插数据
                        Map<String, Object> invuselineMap = new HashMap<>();
                        invuselineMap.put("itemnum", itemnum);
                        invuselineMap.put("model", MapUtil.getStr(lineList.get(k), "model"));//规格型号
                        invuselineMap.put("grantqty", MapUtil.getStr(lineList.get(k), "thenum"));
                        invuselineMap.put("binnum", MapUtil.getStr(lineList.get(k), "frombin"));
                        invuselineMap.put("lotnum", MapUtil.getStr(lineList.get(k), "fromlot"));
                        invuselineMap.put("invusenum", invusenum + 1);
                        invuselineMap.put("itemdesc", MapUtil.getStr(lineList.get(k), "itemdesc"));//物料描述
                        invuselineMap.put("quantity", thenum);//出库数量
                        invuselineMap.put("linetype", MapUtil.getStr(lineList.get(k), "type"));//物料类别

                        handlerService.insert("gq_invuseline", invuselineMap);
                        //往入库记录插数据
                        Map<String, Object> instorageMap = new HashMap<>();
                        Integer instonum = 10000;
                        List<Map<String, Object>> bySql1 = handlerService.findBySql("select instonum from gq_instorage order by instonum desc");
                        if (bySql1 != null && bySql1.size() > 0) {
                            Map<String, Object> map = bySql1.get(0);
                            instonum = MapUtils.getInteger(map, "instonum");
                            instorageMap.put("instonum", instonum + 1);
                        } else {
                            instorageMap.put("instonum", instonum + 1);
                        }
                        instorageMap.put("instodesc", MapUtil.getStr(list.get(0), "transferstodesc"));
                        instorageMap.put("recipient", MapUtil.getStr(list.get(0), "createuserdesc"));
                        instorageMap.put("deptcode", MapUtil.getStr(list.get(0), "deptcode"));
                        instorageMap.put("orgcode", MapUtil.getStr(list.get(0), "orgcode"));
                        instorageMap.put("type", "接收");
                        instorageMap.put("createuser", MapUtil.getStr(list.get(0), "createuser"));
                        instorageMap.put("createuserdesc", MapUtil.getStr(list.get(0), "createuserdesc"));
                        instorageMap.put("createtime", new Date());
                        instorageMap.put("status", "审批通过");


                        //handlerService.insert("gq_instorage", instorageMap);
                        //往入库记录里物料行插数据
                        Map<String, Object> instolineMap = new HashMap<>();
                        instolineMap.put("instonum", instonum + 1);
                        instolineMap.put("itemdesc", MapUtil.getStr(lineList.get(k), "itemdesc"));
                        instolineMap.put("linetype", MapUtil.getStr(lineList.get(k), "type"));
                        instolineMap.put("innum", thenum);
                        instolineMap.put("issuetype", "接收");
                        instolineMap.put("model", MapUtil.getStr(lineList.get(k), "model"));
                        //品牌
//                        instolineMap.put("brand",MapUtil.getInt(lineList.get(k), "model"));
                        instolineMap.put("supply", MapUtil.getStr(lineList.get(k), "supply"));
                        instolineMap.put("storageroom", MapUtil.getStr(lineList.get(k), "supply"));
                        instolineMap.put("itemnum",itemnum);
                        handlerService.insert("gq_instoline", instolineMap);
                    }
                }
            }
            }
//        }
    }*/

    public void beforSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        if ("transferstoApp".equals(app)) {
            if ("gq_transfersto".equals(objName)) {
                if (isUpdate) {
                    //当子表有值时原仓库、目标仓库，领用人不能修改
                    String id = MapUtils.getString(data, "id");
                    Map<String, Object> transferstoMap = transferstoDao.findTransfersto(id);
                    StringJoiner stringJoiner = new StringJoiner(",");
                    String outwarehousenum = MapUtils.getString(transferstoMap, "outwarehousenum");
                    String inwarehousenum = MapUtils.getString(transferstoMap, "inwarehousenum");
                    if (!outwarehousenum.equals(MapUtils.getString(data, "outwarehousenum"))) {
                        stringJoiner.add("转出仓库");
                    }
                    if (!inwarehousenum.equals(MapUtils.getString(data, "inwarehousenum"))) {
                        stringJoiner.add("转入仓库");
                    }
                    if (stringJoiner.length() > 0) {
                        //String transferstonum = MapUtils.getString(data, "transferstonum");
                        String transferstoid = MapUtils.getString(data, "transferstoid");
                        List<String> transferstoitem = transferstoDao.findTransferstoitemList(transferstoid);
                        if (CollectionUtils.isNotEmpty(transferstoitem)) {
                            throw new Exception("当子表有值时'"+stringJoiner+"'不能修改");
                        }
                    }
                }
            }
        }
//        else if ("gq_transferstoitem".equals(objName)){
//            String transferstoid = MapUtils.getString(data, "transferstoid");
//            List<Map<String, Object>> bySql = handlerService.findBySql("select outwarehousenum from gq_transfersto where id='" + transferstoid + "'");
//            Map<String, Object> map = bySql.get(0);
//            String outwarehousenum = MapUtils.getString(map, "outwarehousenum");
//            List<Map<String, Object>> bySql1 = handlerService.findBySql("select * from gq_transferstoitem where transferstoid='" + transferstoid + "'");
//            if (CollectionUtils.isNotEmpty(bySql1)){
//                for (int j=0;j<bySql1.size();j++){
//                    Map<String, Object> map1 = bySql1.get(j);
//                    String itemnum = MapUtils.getString(map1, "itemnum");
//                    String fromlot = MapUtils.getString(map1, "fromlot");
//                    List<Map<String, Object>> bySql2 = handlerService.findBySql("SELECT version from gq_invbalances where itemnum='" + itemnum + "' and storenum='" + outwarehousenum + "' and batchnum='" + fromlot + "'");
//                    if (CollectionUtils.isNotEmpty(bySql2)){
//                        Map<String, Object> map2 = bySql2.get(0);
//                        String version = MapUtils.getString(map2, "version");
//                        String versions = MapUtils.getString(data, "versions");
//                        if (!version.equals(versions)){
//                            throw new ServiceException("库存可用数量已发生改变请重新选择物料！");
//                        }
//                    }
//                }
//            }
//        }
    }

    @Override
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
            if ("transferstoApp".equals(app) || "transfersto1App".equals(app)){
                if ("gq_transferstoitem".equals(objName)){
                    String transferstoid = MapUtils.getString(data, "transferstoid");
                    List<Map<String, Object>> bySql = handlerService.findBySql("SELECT thenum,outprice from gq_transferstoitem where transferstoid ='" + transferstoid + "'\n");
                    BigDecimal bigDecimal = new BigDecimal("0");
//                    float outcost= 0;
                    if (CollectionUtils.isNotEmpty(bySql)){
                        for (int v=0;v<bySql.size();v++){
                            Map<String, Object> map = bySql.get(v);
                            String thenum = MapUtils.getString(map, "thenum");
                            String outprice = MapUtils.getString(map, "outprice");
                            if(StringUtils.isEmpty(outprice)){
                                outprice="0";
                            }
                            BigDecimal multiply = new BigDecimal(thenum).multiply(new BigDecimal(outprice));
                            bigDecimal = bigDecimal.add(multiply);
                        }
                    }
                    handlerService.updateBySql("UPDATE gq_transfersto set outtotalcost = '" + bigDecimal.toString() + "' WHERE id ='"+transferstoid+"'");
                    if (isUpdate==true){
                        String id = MapUtils.getString(data, "id");
                        List<Map<String, Object>> bySql1 = handlerService.findBySql("select * from gq_transferstoitem where id=" + id);
                        if (CollectionUtils.isNotEmpty(bySql1)){
                            Map<String, Object> map = bySql1.get(0);
                            String subjectno = MapUtils.getString(map, "subjectno");
                            String transferstoid1 = MapUtils.getString(map, "transferstoid");
                            List<Map<String, Object>> bySql2 = handlerService.findBySql("select * from gq_transfersto where id='" + transferstoid1 + "'");
                            if (CollectionUtils.isNotEmpty(bySql2)){
                                Map<String, Object> map1 = bySql2.get(0);
                                String primarysubjectno = MapUtils.getString(map1, "primarysubjectno");
                                if (!primarysubjectno.equals(subjectno)){
                                    handlerService.updateBySql("update gq_transfersto set collectout =1 where id='"+transferstoid1+"'");
                                }else {
                                    handlerService.updateBySql("update gq_transfersto set collectout =0 where id='"+transferstoid1+"'");
                                }
                            }

                        }
                    }else {
                        String id = MapUtils.getString(data, "id");
                        List<Map<String, Object>> bySql1 = handlerService.findBySql("select * from gq_transferstoitem where id=" + id);
                        if (CollectionUtils.isNotEmpty(bySql1)){
                            Map<String, Object> map = bySql1.get(0);
                            String subjectno = MapUtils.getString(map, "subjectno");
                            String transferstoid1 = MapUtils.getString(map, "transferstoid");
                            List<Map<String, Object>> bySql2 = handlerService.findBySql("select * from gq_transfersto where id='" + transferstoid1 + "'");
                            if (CollectionUtils.isNotEmpty(bySql2)){
                                Map<String, Object> map1 = bySql2.get(0);
                                String primarysubjectno = MapUtils.getString(map1, "primarysubjectno");
                                if (!primarysubjectno.equals(subjectno)){
                                    handlerService.updateBySql("update gq_transfersto set collectout =1 where id='"+transferstoid1+"'");
                                }else {
                                    handlerService.updateBySql("update gq_transfersto set collectout =0 where id='"+transferstoid1+"'");
                                }
                            }

                        }
                    }
                }
            }
//        String transferstonum=MapUtil.getStr(data,"transferstonum");
//        List<Map<String, Object>> linelist = handlerService.findBySql("select ifnull(sum(transfercost),0) cost from bl_transferstoitem  where transferstonum='"+transferstonum+"' ");
//        if(linelist!=null && linelist.size()>0){
//            String  cost =MapUtil.getStr(linelist.get(0),"cost");
//            handlerService.findBySql("update bl_transfersto set outtotalcost='"+cost+"' where transferstonum ='"+transferstonum+"'");
//        }
        super.afterSave(app, objName, data, isUpdate);
    }

    @Override
    public String validateSave(String app, String objName, Map<String, Object> data) throws Exception {
        if("bl_transfersto".equals(objName)){
            String outwarehousenum=MapUtil.getStr(data,"outwarehousenum"); //转出仓库编码
            String inwarehousenum=MapUtil.getStr(data,"inwarehousenum"); // 转入仓库编码
            if(outwarehousenum.equals(inwarehousenum)){
                return "转出仓库不能于转入仓库相同";
            }
        }


        return super.validateSave(app, objName, data);
    }
}
